<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
</head>

<body>
	<script>
		let url = 'http://www.nowcoder.com?key=1&key=2&key=3&test=4&a&key#hehe'
		// let url = 'http://www.nowcoder.com?'
		function getUrlParam(sUrl, sKey) {
			let param = sUrl.split('#')[0].split('?')[1]

			let _arr = []
			if (param) {
				let arr = param.split('&')

				if (sKey) {
					arr.forEach(val => {
						let _val = val.split('=')
						if (_val.length == 1) {
							_val[1] = ''
						}
						if (_val[0] == sKey) {
							_arr.push(_val[1])
						}
					});
					return _arr.length == 1 ? _arr[0] : _arr
				} else {
					let obj = {}
					for (let i = 0; i < arr.length; i++) {
						let val = arr[i].split('=')
						if (val.length == 1) {
							val[1] = ''
						}
						obj[val[0]] = (obj[val[0]] == undefined ? [] : obj[val[0]]).concat(val[1])
					}
					for (let i = 0; i < Object.keys(obj).length; i++) {
						if (obj[Object.keys(obj)[i]].length == 1) {
							obj[Object.keys(obj)[i]] = obj[Object.keys(obj)[i]][0]
						}
					}
					return obj
				}
			} else {
				if (sKey) {
					return ''
				} else {
					return {}
				}
			}
		}
		console.log(getUrlParam(url, 'key'));
		// console.log(getUrlParam(url));

	</script>
</body>

</html>